
package com.yuan.localServer;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.yuan.control.CheckControl;
import com.yuan.control.SettingControl;
import com.yuan.model.M员工活动轨迹表;
import com.yuan.prefence.Constants;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.PreferenceUtil;
import com.yuan.unit.TimeUtils;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;

/**
 * 定位服务类
 * 
 * @author YUAN
 */
public class LocalServer extends Service implements
        AMapLocationListener, PreferenceKeys {
    private LocationManagerProxy aMapLocManager = null;
    private Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        aMapLocManager = LocationManagerProxy.getInstance(this);
        aMapLocManager.requestLocationUpdates(
                LocationProviderProxy.AMapNetwork, 1000 * 60 * 10, 10, this);
        mContext = this;
        AsyncDataJobHandler.getInstance().bindContext(this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        stopLocation();
    }

    /**
     * 销毁定位
     */
    private void stopLocation() {
        if (aMapLocManager != null) {
            aMapLocManager.removeUpdates(this);
            aMapLocManager.destory();
        }
        aMapLocManager = null;
    }

    /**
     * 此方法已经废弃
     */
    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    /**
     * 混合定位回调函数
     */
    @Override
    public void onLocationChanged(AMapLocation location) {
        Bundle locBundle = location.getExtras();
        LocalManage.getInstance().adress = locBundle.getString("desc");
        String city = PreferenceUtil.getInstance().getString(USER_CITY, "");
        if (!location.getCity().equals(city))
        {
            PreferenceUtil.getInstance().putString(USER_CITY, location.getCity());
        }
        double lng = LocalManage.getInstance().lng;
        double lat = LocalManage.getInstance().lat;
        updateLocation(lat, lng);
        LatLng last = new LatLng(lat, lng);
        double geoLat = location.getLatitude();
        double geoLng = location.getLongitude();
        LatLng now = new LatLng(geoLat, geoLng);
        if (Math.abs(AMapUtils.calculateLineDistance(last, now)) > Constants.DIATANCE) {
            LocalManage.getInstance().lat = geoLat;
            LocalManage.getInstance().lng = geoLng;
            PreferenceUtil.getInstance().putFloat(PreferenceKeys.LAT,
                    (float) geoLat);
            PreferenceUtil.getInstance().putFloat(PreferenceKeys.LNG,
                    (float) geoLng);
            LocalManage.getInstance().onNoticeLocalChanged(location);
        }
        String str = TimeUtils.getNowTime();
        if ((!CheckControl.isCheckedInToday()) && SettingControl.IsAutoCkeckIn()
                && (!CheckControl.isRightChechinTime(str)))
        {// 如果今天上班没有签到 并且开启了上班自动签到 并且现在的事前大于上班时间
            CheckControl.setCheckIn(mContext, new Handler());
        }
        if ((!CheckControl.isCheckedOutToday()) && SettingControl.IsAutoCkeckOut()
                && (CheckControl.isRightChechoutTime(str)))
        {// 如果今天下班没有签到 并且开启了下班自动签到 并且现在的时间大于下班时间
            CheckControl.setCheckOut(mContext, new Handler());
        }

    }

    private void updateLocation(double lat, double lng) {
        // TODO Auto-generated method stub
        M员工活动轨迹表.getInstance().setLat(lat);
        M员工活动轨迹表.getInstance().setLng(lng);
        AsyncDataJobHandler.getInstance().updateLocation();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}
